package com.welsy.iter.vo.iter {
    import flash.display.Sprite;
    import flash.events.NetStatusEvent;
    import flash.events.SecurityErrorEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.events.Event;
    import mx.charts.chartClasses.StackedSeries;
    import mx.controls.Alert;
    
    public class StreamingVideo extends Sprite {
        public var videoURL:String;
        public var connection:NetConnection;
        public var stream:NetStream;
        public var video:Video = new Video();
        public var videoEnded:Boolean;

        public function StreamingVideo(URLvideo:String) {
        	if (URLvideo == "") {
        		//default video
        		URLvideo = "http://10.12.150.106/fileserver/0/test.flv"
        	}
        	videoURL = URLvideo;        	   
        }
        
        public function StartStream():void {
        	connection = new NetConnection();
            connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            connection.connect(null);
        }

        public function netStatusHandler(event:NetStatusEvent):void {        	
            switch (event.info.code) {
                case "NetConnection.Connect.Success":
                    connectStream();
                    break;
                case "NetStream.Play.StreamNotFound":
                    trace("Stream not found: " + videoURL);
                    break;
                case "NetStream.Play.Start":
                	videoEnded = false;
                    break;
                case "NetStream.Play.Stop":
                	videoEnded = true;
                	trace("NetStream.Play.Stop");
                    break;
            }
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        public function connectStream():void {
            stream = new NetStream(connection);
            stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            stream.client = new CustomClient();            
            video.attachNetStream(stream);
            stream.play(videoURL);            
            addChild(video);
        }
    }
}

class CustomClient {
    public function onMetaData(info:Object):void {
        trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
    }
    public function onCuePoint(info:Object):void {
        trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
    }
}